﻿using System.ComponentModel;
using System.Windows.Media;
using LunarPhase.Model;
using System.Windows.Media.Imaging;
using System;
using System.Globalization;
using LunarPhase.Gadget.Res;
using System.Windows.Threading;

namespace LunarPhase.Gadget.ViewModel
{
    public class LunarWindowViewModel : INotifyPropertyChanged
    {
        private DispatcherTimer moonRefreshTimer;
        private Uri currentMoonImage;
        private string percLight;
        private string moonName;
        private Uri signImage;
        private string signName;

        #region Public Properties

        public Moon CurrentMoon { get; private set; }

        public string MoonName
        {
            get
            {
                return moonName;
            }
            set
            {
                moonName = value;
                PropertyChanged.Raise(() => this.MoonName);
            }
        }

        public string PercLight
        {
            get
            {
                return this.percLight;
            }
            set
            {
                this.percLight = value;
                PropertyChanged.Raise(() => this.PercLight);
            }
        }

        public Uri CurrentMoonImage
        {
            get
            {
                return currentMoonImage;
            }
            set
            {
                currentMoonImage = value;
                PropertyChanged.Raise(() => this.CurrentMoonImage);
            }
        }

        public Uri SignImage
        {
            get
            {
                return this.signImage;
            }
            set
            {
                this.signImage = value;
                PropertyChanged.Raise(() => this.SignImage);
            }
        }

        public string SignName
        {
            get
            {
                return this.signName;
            }
            set
            {
                this.signName = value;
                PropertyChanged.Raise(() => this.SignName);
            }
        }

        #endregion

        public LunarWindowViewModel()
        {
            // this.CurrentMoon = new Moon(new DateTime(2010,03,23));
            this.CurrentMoon = new Moon();
            this.Initialize();
        }

        public LunarWindowViewModel(Moon moon)
        {
            this.CurrentMoon = moon;
            this.Initialize();
        }

        private void Initialize()
        {
            int absoluteMoonAge = (int)System.Math.Round(CurrentMoon.MoonAge);
            absoluteMoonAge++;
            string uri = string.Format("../Images/Moon{0}.png", absoluteMoonAge.ToString());
            this.CurrentMoonImage = new Uri(uri, UriKind.RelativeOrAbsolute);
            this.PercLight = string.Concat(this.CurrentMoon.PercentageIlluminated.ToString(), CultureInfo.CurrentCulture.NumberFormat.PercentSymbol);
            this.MoonName = LunarWindoViewModelResources.ResourceManager.GetString(this.CurrentMoon.Phase.ToString());
            this.SignImage = new Uri(string.Format("../Images/{0}.png", this.CurrentMoon.Sign), UriKind.RelativeOrAbsolute);
            this.SignName = LunarWindoViewModelResources.ResourceManager.GetString(this.CurrentMoon.Sign.ToString());
            this.ConfigureMoonRefreshTimer();
        }

        private void ConfigureMoonRefreshTimer()
        {
            this.moonRefreshTimer = new DispatcherTimer();
            if (!this.MoonRefreshTimerInitialized)
            {
                moonRefreshTimer.Tick +=
                                        delegate(object s, EventArgs args)
                                        {
                                            //if (this.CurrentMoon.Date.ToShortDateString() != DateTime.Now.ToShortDateString())
                                            //{
                                            this.CurrentMoon = new Moon();
                                            this.Initialize();
                                            //}
                                        };
                this.MoonRefreshTimerInitialized = true;
            }
            moonRefreshTimer.Interval = new TimeSpan(0, 2, 30);
            moonRefreshTimer.Start();
        }

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        #endregion

        private bool MoonRefreshTimerInitialized { get; set; }

    }
}
